package com.zzyl.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.exception.BusinessException;
import com.zzyl.service.NursingProjectPlanService;
import com.zzyl.service.NursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 护理项目表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-15
 */
@RestController
public class NursingProjectController extends BaseController {

    @Autowired
    private NursingProjectService nursingProjectService;
    @Autowired
    private NursingProjectPlan nursingProjectPlan;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    //分页查询
    @GetMapping("/nursing_project")
    public ResponseResult findByIdPage(String name,Integer status,
                                       @RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10")Integer pageSize){
        PageResponse<NursingProject> pageResponse = nursingProjectService.findByIdPage(name,status,pageNum,pageSize);
        return success(pageResponse);
    }

    //新增
    @PostMapping("/nursing_project")
    public ResponseResult save(@RequestBody  NursingProject nursingProject){
        nursingProjectService.save(nursingProject);
        return success();
    }

    //主键查询
    @GetMapping("nursing_project/{id}")
    public ResponseResult findById(@PathVariable Long id){
        NursingProject nursingProject = nursingProjectService.getById(id);
        return success(nursingProject);
    }

    //编辑
    @PutMapping("nursing_project/{id}")
    public ResponseResult update(@RequestBody NursingProject nursingProject){
        nursingProjectService.updateById(nursingProject);
        return success();
    }

    //启用禁用·
    @PutMapping("nursing_project/{id}/status/{status}")
    public ResponseResult switchStatus(@PathVariable Long id,@PathVariable Integer status){
        nursingProjectService.switchStatus(id,status);
        return success();
    }

    // todo: 删除
    @DeleteMapping("nursing_project/{id}")
    public ResponseResult delete(@PathVariable Long id){

        //如果项目被引用，则无法删除
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getProjectId,id);
        NursingProjectPlan byId = nursingProjectPlanService.getOne(wrapper);
        if (byId != null){
            throw new BusinessException("项目被引用，无法删除");
        }
        nursingProjectService.removeById(id);
        return success();
    }

    //查询所有
    @GetMapping("/nursing_project/all")
    public ResponseResult findByIdPage(){
         List<NursingProject> list = nursingProjectService.list();
        return success(list);
    }
}
